package com.aiwei.tdjk.bean;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Bean类 - 分页
 */

@SuppressWarnings("unchecked")
public class Pager<T> {
	
	// 排序方式
	public enum OrderType{
		asc, desc
	}
	
	public static final Integer MAX_PAGE_SIZE = 500;// 每页最大记录数限制

	public static final Integer PAGE_SIZE = 10;//

	private Integer pageNumber = 1;// 当前页码

	private Integer pageOffset = 1;// 当前

	private Integer pageSize = PAGE_SIZE;// 每页记录数

	private Integer totalCount = 0;// 总记录数

	private Integer totalPages = 0;// 总页数

	private String property;// 查找属性名称

	private String keyword;// 查找关键字

	private String orderBy = "id";// 排序字段

	private OrderType orderType = OrderType.desc;// 排序方式

	private List<T> list;// 数据List

	private T[] data;//s


	public void webParamConvert() {
		if (pageSize != null && pageNumber != null) {
			pageOffset = pageSize * (pageNumber - 1);
		}
	}

	public void init(HttpServletRequest request) {
		int pageNumber = Integer.parseInt(StringUtils.defaultIfBlank(request.getParameter("pageNumber"), "1"));
		setPageNumber(pageNumber);
		int pageSize = Integer.parseInt(StringUtils.defaultIfBlank(request.getParameter("pageSize"), String.valueOf(PAGE_SIZE)));
		setPageSize(pageSize);
		webParamConvert();
	}

	public Integer getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(Integer pageNumber) {
		if (pageNumber < 1) {
			pageNumber = 1;
		}
		this.pageNumber = pageNumber;
	}
	
	public Integer getPageOffset() {
		return pageOffset;
	}

	public void setPageOffset(Integer pageOffset) {
		this.pageOffset = pageOffset;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		if (pageSize < 1) {
			pageSize = 1;
		} else if(pageSize > MAX_PAGE_SIZE) {
			pageSize = MAX_PAGE_SIZE;
		}
		this.pageSize = pageSize;
	}
	
	public Integer getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
		setTotalPages(getTotalPages());
	}

	public Integer getTotalPages() {
		totalPages = totalCount / pageSize;
		if (totalCount % pageSize > 0) {
			totalPages ++;
		}
		return totalPages;
	}

	public void setTotalPages(Integer pageCount) {
		this.totalPages = pageCount;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	
	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	
	public OrderType getOrderType() {
		return orderType;
	}

	public void setOrderType(OrderType orderType) {
		this.orderType = orderType;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public T[] getData() {
		return data;
	}

	public void setData(T[] data) {
		this.data = data;
	}

}